var CommunicationNode = function(x, y, stage){
    var _shape;
    var _x;
    var _y;
    var _waitingToBeProcessedPackets = new Array();
    var _connexions = new Array();
    var _chosenConnexion;
    var _image;
    

    var _skipToNextAvailableConnexion = function(){
        var changed = false;
        for(var i = 0; i<_connexions.length; i++){
            
            if(_chosenConnexion == _connexions[i] && !changed){
                if(i+1 >= _connexions.length){
                    _chosenConnexion = _connexions[0];
                } else {
                    _chosenConnexion = _connexions[i+1];
                }
                changed = true;
            }
        }
        SoundEngine.getInstance().nodeSwitch();
    }
    
    
    _shape = new createjs.Shape();
    
    var _rotateShape = function(){
        if (_chosenConnexion instanceof Connexion){
            var destination = _chosenConnexion.getOppositePointFromXY(_x, _y);
            
            var diffX = destination.getX()- _x;
            var diffY = destination.getY()- _y;
            
            var angle = (Math.atan(diffY/diffX))*180/Math.PI;
            if(diffX < 0){
                angle = angle - 180;
            }
            
            _shape.rotation = angle;
        }
    }
    
    _x = x;
    _y = y;
    
    // Generate image.
    _image = ImageEngine.getInstance().getNode();
    
    // Center the element.
    _shape.x = _x //- _image.width/2;
    _shape.y = _y //- _image.height/2;
    _shape.regX = _image.width/2;
    _shape.regY = _image.height/2;

    
    // Generate the graphic element.
    _shape.graphics.beginBitmapFill(_image).drawRect(0, 0, _image.width, _image.height);
    _shape.onClick = function(evt){
        _skipToNextAvailableConnexion();
        _rotateShape();
        ClickCounter.getInstance().incrementClick();
    };
    // Add the shape.
    stage.addChild(_shape);
    
    this.getShape = function(){
        return _shape
    };
    this.getX = function(){
        return _x
    };
    this.getY = function(){
        return _y
    };
    
    this.addConnexion = function(connexion){
        _connexions.push(connexion);
        _chosenConnexion = connexion;
        _rotateShape();
    }
    this.getConnexion = function() {
        return _connexion;
    }
  
    this.setChosenConnexion = function(cnx){
        _chosenConnexion = cnx;
        _rotateShape();
    }
    
    
      
    this.addPacketToWaitingList = function(packet){
        _waitingToBeProcessedPackets.push(packet);
    }
    
    this.processWaitingPacket = function(){
        var packet;
        for(var pIndex in _waitingToBeProcessedPackets){
            packet = _waitingToBeProcessedPackets[pIndex];
            
            packet.decrementHealth();
            if(packet.getHealth() <= 0){
                packet.destroy();
            } else {
                packet.setX(_x);
                packet.setY(_y);
                _chosenConnexion.addTranslation(new Translation(packet, _chosenConnexion.getOppositePointFrom(this)));
            }
            SoundEngine.getInstance().nodePass();
            _waitingToBeProcessedPackets.splice(pIndex, 1);
        }
        
    };
}